package pri.syl.xiaowu.websocket.netty.server.handler.core.strategy;
import io.netty.handler.codec.http.websocketx.*;
import  pri.syl.xiaowu.websocket.core.template.WebsocketChannelTemplate;
import  pri.syl.xiaowu.websocket.core.template.WebsocketMessageTemplate;
import  pri.syl.xiaowu.websocket.core.template.WebsocketRouteTemplate;

/**
 * ClassName: MessageEventStrategy
 * Description: websocket消息事件策略
 */
public class MessageEventStrategy {
    private MessageEventStrategy() {
    }

    public static void messageEvent(WebsocketRouteTemplate websocketRouteTemplate, WebsocketChannelTemplate websocketChannelTemplate, WebSocketFrame frame) {
        WebsocketMessageTemplate websocketMessageTemplate = new WebsocketMessageTemplate(frame);
        if (frame instanceof PingWebSocketFrame) {
            websocketRouteTemplate.pingMessageEvent (websocketChannelTemplate, websocketMessageTemplate);
        }
        if (frame instanceof PongWebSocketFrame) {
            websocketRouteTemplate.pongMessageEvent (websocketChannelTemplate, websocketMessageTemplate);
        }
        if (frame instanceof TextWebSocketFrame) {
            websocketRouteTemplate.textMessageEvent (websocketChannelTemplate, websocketMessageTemplate);
        }
        if (frame instanceof BinaryWebSocketFrame) {
            websocketRouteTemplate.binaryMessageEvent (websocketChannelTemplate, websocketMessageTemplate);
        }
        if (frame instanceof CloseWebSocketFrame) {
            websocketRouteTemplate.closeMessageEvent (websocketChannelTemplate, websocketMessageTemplate);
        }
        frame.release ();

    }
}
